<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script>
        //1.遍历数组
        var arr = ['apple', 'orange', 'lizi', 'banana']
        for (i = 0; i < arr.length; i++) {
            console.log(arr[i]);
        }

        //2.求和和平均数
        var arr = [2, 6, 1, 7, 4]
        sum = 0;
        average = 0;
        for (i = 0; i < arr.length; i++) {
            sum += arr[i];
        }
        average = sum / arr.length;
        console.log('2.和为:' + sum + '平均数为:' + average);

        //3.求数组最大数
        var arr = [1, 2, 10, 56, 79, 43, 88, 99];
        var max = arr[0];
        var min = arr[0];
        for (var i = 1; i < arr.length; i++) {
            if (arr[i] > max) {
                max = arr[i];
            }
            if (arr[i] < min) {
                min = arr[i]
            }
        }
        console.log('3.数组的最大值为:' + max + '最小值为:' + min);

        //4.筛选数组
        var arr = [2, 0, 6, 1, 77, 0, 53, 25, 0, 7];
        var newArr = [];
        for (i = 0; i < arr.length; i++) {
            if (arr[i] > 20) {
                newArr[newArr.length] = arr[i];
            }
        }
        console.log("4.原数组中大于20的元素有:" + newArr);

        //5.数组去重
        var arr = [2, 0, 6, 1, 77, 0, 7, 1, 0, 7];
        var newArr = [];
        for (i = 0; i < arr.length; i++) {
            //遍历要去重的数组，当值不在新数组时(indexOf为-1)，就push加入新数组
            if (newArr.indexOf(arr[i]) == -1) {
                newArr.push(arr[i]);
            }
        }
        console.log('5.原数组去重后的结果为:' + newArr);

        //6.翻转数组
        var arr = ['red', 'blue', 'blank', 'yellow'];
        var newArr = [];
        for (i = arr.length - 1; i >= 0; i--) {
            newArr[newArr.length] = arr[i];
        }
        console.log('6.翻转后的数组为:' + newArr);

        //7.冒泡排序
        var arr = [25, 78, 43, 22, 56, 32, 22, 76, 56];
        for (i = 0; i <= arr.length - 1; i++) { //外层循环管躺数
            for (j = 0; j <= arr.length - i - 1; j++) { //里面的循环 管 每一趟的交换次数
                if (arr[j] > arr[j + 1]) {
                    var temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
        }
        console.log('7.冒泡排序后的数组为:' + arr);
    </script>
</head>

<body>

</body>

</html>